const http = require('http');
const WebSocket = require('ws')
const axios = require('axios')
const marked = require('marked')


const wss = new WebSocket.Server({port:8080})

const axiosInstance = axios.create({
    baseUrl: 'https://api.openai.com/v1/completions',
    headers :{
        'Content-Type': 'application/json',
        'Authorization:': 'Bearer your openai key'
    }
})

wss.on('connection', ws=>{
    ws.on('message', message=>{
        console.log(`Received message => ${message}`)
        let sendMes = 'Hello, This message is from server, I will code Hello World by java:\n'+
        '<pre>public class HelloWorld {\n\n    public static void main(String[]args){\n\n        System.out.println("Hello World");\n    }\n}\n</pre>\n'+
        'Welcome use me!'
        ws.send('请稍后，正在生成……')
        request.post('/', JSON.stringify(params))
            .then(res => {
                res.data.choices.forEach(element => {
                    ws.send(element.text)
                });
            })
            .catch(err => {
                ws.send(`<span style='color:red;'>发生异常: ${err.data}</span>`)
            })
    })
})